﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

public class Player : Characters
{
    public enum PlayerNumber { ONE, TWO }

    public Player.PlayerNumber player;

    public Player(Player.PlayerNumber player, float speed, float friction, Vector2 position)
        : base(speed, friction, position)
    {
        Life = 5;
        this.player = player;
    }


    /// <summary>
    /// Handles input, performs physics, and animates the player sprite.
    /// </summary>
    public override void Update(GameTime gameTime)
    {
        if (State != States.Dead)
        {
            GetInput();
            base.Update(gameTime);

            ApplyPhysics(true);
        }
    }

    protected override void ApplyPhysics(bool checkall)
    {
        Vector2 previousPosition = Position;

        Rectangle previousBounding = BoundingRectangle;

        base.ApplyPhysics(/*gameTime*/ checkall);

        GameSession.HandleCollisions(Position, previousPosition, previousBounding, BoundingRectangle, out position, true, this);

        if (Position.X == previousPosition.X)
            Velocity.X = 0;

        if (Position.Y == previousPosition.Y)
            Velocity.Y = 0;

    }

    /// <summary>
    /// Gets player movement and commands from input.
    /// </summary>
    protected void GetInput()
    {
        if (player == PlayerNumber.ONE)
        {
            if (InputManager.IsActionPressed(InputManager.Action.MovePlayer1Down))
            {
                Velocity.Y += Speed;
				_Direction = Direction.Down;
                ApplyPhysics(/*gameTime*/ true);
            }
            if (InputManager.IsActionPressed(InputManager.Action.MovePlayer1Up))
            {
                Velocity.Y -= Speed;
				_Direction = Direction.Up;
                
                ApplyPhysics(/*gameTime*/ true);
            }
            if (InputManager.IsActionPressed(InputManager.Action.MovePlayer1Right))
            {
                Velocity.X += Speed;
                _Direction = Direction.Right;
                ApplyPhysics(/*gameTime*/ true);
            }
            if (InputManager.IsActionPressed(InputManager.Action.MovePlayer1Left))
            {
                Velocity.X -= Speed;
                _Direction = Direction.Left;
                ApplyPhysics(/*gameTime*/ true);
            }
            if(HasBarrel && InputManager.IsActionPressed(InputManager.Action.Player1ThrowBarrel))
            {
                _Barrel.DoABarrelRoll(_Direction);
                HasBarrel = false;
            }
        }
        else
        {
            if (InputManager.IsActionPressed(InputManager.Action.MovePlayer2Down))
            {
                Velocity.Y += Speed;
                _Direction = Direction.Down;
                ApplyPhysics(/*gameTime*/ true);
            }
            if (InputManager.IsActionPressed(InputManager.Action.MovePlayer2Up))
            {

                Velocity.Y -= Speed;
                _Direction = Direction.Up;

                ApplyPhysics(/*gameTime*/ true);
            }
            if (InputManager.IsActionPressed(InputManager.Action.MovePlayer2Right))
            {
                Velocity.X += Speed;
                _Direction = Direction.Right;
                ApplyPhysics(/*gameTime*/ true);
            }
            if (InputManager.IsActionPressed(InputManager.Action.MovePlayer2Left))
            {
                Velocity.X -= Speed;
                _Direction = Direction.Left;
                ApplyPhysics(/*gameTime*/ true);
            }
            if (HasBarrel && InputManager.IsActionPressed(InputManager.Action.Player2ThrowBarrel))
            {
                _Barrel.DoABarrelRoll(_Direction);
                HasBarrel = false;
            }
        }

        //ApplyPhysics(/*gameTime*/ true);
    }

    private bool Collision()
    {
        if (BoundingRectangle.Y < 0 || BoundingRectangle.Y + BoundingRectangle.Height >= 720 || BoundingRectangle.X < 0 || BoundingRectangle.X + BoundingRectangle.Width >= 1280)
            return true;
        else
            if (GameSession.CollisionMatriz[(int)BoundingRectangle.X / 64, (int)(BoundingRectangle.Y) / 64] == 1 ||
                GameSession.CollisionMatriz[(int)BoundingRectangle.X / 64, (int)(BoundingRectangle.Y + BoundingRectangle.Height) / 64] == 1 ||
                GameSession.CollisionMatriz[(int)(BoundingRectangle.X + BoundingRectangle.Width) / 64, (int)(BoundingRectangle.Y) / 64] == 1 ||
                GameSession.CollisionMatriz[(int)(BoundingRectangle.X + BoundingRectangle.Width) / 64, (int)(BoundingRectangle.Y + BoundingRectangle.Height) / 64] == 1)
            {
                return true;
            }
            else
            {
                int posx = 0, posy = 0;

                if (GameSession.CollisionMatriz[(int)BoundingRectangle.X / 64, (int)(BoundingRectangle.Y) / 64] == 2)
                {
                    posx = (int) ( BoundingRectangle.X ) / 64;
                    posy = (int) ( BoundingRectangle.Y ) / 64;
                }
                if (GameSession.CollisionMatriz[(int)BoundingRectangle.X / 64, (int)(BoundingRectangle.Y + BoundingRectangle.Height) / 64] == 2)
                {
                    posx = (int)(BoundingRectangle.X) / 64;
                    posy = (int)(BoundingRectangle.Y + BoundingRectangle.Height) / 64;
                }
                if (GameSession.CollisionMatriz[(int)(BoundingRectangle.X + BoundingRectangle.Width) / 64, (int)(BoundingRectangle.Y) / 64] == 2)
                {
                    posx = (int)(BoundingRectangle.X + BoundingRectangle.Width) / 64;
                    posy = (int)(BoundingRectangle.Y) / 64;
                }
                if (GameSession.CollisionMatriz[(int)(BoundingRectangle.X + BoundingRectangle.Width) / 64, (int)(BoundingRectangle.Y + BoundingRectangle.Height) / 64] == 2)
                {
                    posx = (int)(BoundingRectangle.X + BoundingRectangle.Width) / 64;
                    posy = (int)(BoundingRectangle.Y + BoundingRectangle.Height) / 64;
                }

                if(posx != 0)
                    foreach(Objects obj in GameSession.ObjectsList)
                    {
                        if (obj.XPosition == posx && obj.YPosition == posy)
                        {
                            if (obj.CanCollide)
                            {
                                return true;
                            }
                        }
                    }

            }

        return false;
    }

}